﻿/********************************************************************************************
 * 文件名称:	Nation.cs
 * 设计人员:	yan(yanxianghui@gmail.com)
 * 设计时间:	2009-03-16
 * 功能描述:	管理员
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Shanfree.Framework.Utility;
using Shanfree.SharpMovie.Model;
using Shanfree.SharpMovie.IDAL;

namespace Shanfree.SharpMovie.SQLServerDAL
{
    /// <summary>
    /// 管理员
    /// </summary>
    public class ListItem : DBPersistenceBase<ListItemInfo>, IListItem
    {

        #region SQL
        private const string SQL_SELECT_LISTID = @"SELECT *
                                                                                FROM [dbo].[V_ListItem] WHERE ListID = @ListID";
        private const string SQL_UPDATE_MOVIEID = @"UPDATE [SharpMovie].[dbo].[ListItem]
                                                                                       SET [SortIndex] = @SortIndex
                                                                                          ,[Remark] = @Remark
                                                                                     WHERE ID = @ItemID";
        private const string SQL_COUNT_LISTID = @"SELECT count(1)
                                                                                FROM [dbo].[ListItem] WHERE ListID = @ListID";

        #endregion

        protected override string ConnectionString
        {
            get
            {
                return DBConnection.ConnectionString;
            }
        }

        /// <summary>
        /// 根据列表ID获得影片项
        /// </summary>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns></returns>
        public IList<ListItemInfo> GetList(string listID, int pageSize, int pageIndex)
        {
            string pagedSQL = CommonHelper.GetPagedSQL(SQL_SELECT_LISTID, "SortIndex", false, pageSize, pageIndex);
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@ListID", listID);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, pagedSQL, param);
            IList<ListItemInfo> list = GetListFromReader(reader);
            CustomList<ListItemInfo> cList = PageAmoutHelper<ListItemInfo>.GetCutomList(this.ConnectionString, SQL_SELECT_LISTID, param, pageSize, list);
            return cList;
        }

        protected override ListItemInfo WriteExtendProperty(SqlDataReader reader, ListItemInfo t)
        {
            t.MovieName = Convert.ToString(reader["MovieName"]);
            t.IMDB_ID = Convert.ToString(reader["IMDB_ID"]);
            return base.WriteExtendProperty(reader, t);
        }


        /// <summary>
        /// 更新收藏项
        /// </summary>
        /// <param name="itemID">收藏分类项主键</param>
        /// <param name="movieID">电影主键</param>
        /// <param name="remark">项注释</param>
        /// <param name="sortIndex">排序号</param>
        public void Update(string itemID, string remark, int sortIndex)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@ItemID", itemID);
            param[1] = new SqlParameter("@Remark", remark);
            param[2] = new SqlParameter("@SortIndex", sortIndex);
            SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.Text, SQL_UPDATE_MOVIEID, param);
        }
        /// <summary>
        /// 计算分类中影片个数
        /// </summary>
        /// <param name="listID">收藏主键</param>
        /// <returns></returns>
        public int Count(string listID)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@ListID", listID);
            object result = SqlHelper.ExecuteScalar(this.ConnectionString, CommandType.Text, SQL_COUNT_LISTID, param);
            return Convert.ToInt32(result);
        }

    }
}
